package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dao.OmcRoomAssignmentMapper;
import com.csun.cmny.provider.model.domain.OmcSchedul;
import com.csun.cmny.provider.model.domain.OmcShiftLog;
import com.csun.cmny.provider.model.domain.OmcShifts;
import com.csun.cmny.provider.model.dto.ShiftLogDto;
import com.csun.cmny.provider.model.vo.DeviceAlertReqVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcShiftLogService;
import com.csun.cmny.provider.validator.ShiftLogDtoAddValid;
import com.csun.cmny.provider.validator.ShiftLogDtoEditValid;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;


@RestController
@RequestMapping(value = "/shiftLog", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcShiftLogController extends BaseController {

    @Resource
    private OmcShiftLogService omcShiftLogService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcRoomAssignmentMapper omcRoomAssignmentMapper;

    /**
     * 查询出进行交接班的班次
     * @param: [shiftLogDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation
    @PostMapping(value = "/selectPlans")
    public Wrapper queryShiftsList(@RequestBody ShiftLogDto shiftLogDto,
                                 BindingResult br) {

        List<OmcShifts> omcShiftsList = omcShiftLogService.queryShiftsList(convert(shiftLogDto),getLoginAuthDto());

        //return WrapMapper.ok(omcShiftsList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),omcShiftsList);
    }

    @PostMapping(value = "/selectPlansMonth")
    public Wrapper queryShiftsListMonth(@RequestBody ShiftLogDto shiftLogDto) {

        //创建查询对象
        OmcSchedul omcSchedulQuery = new OmcSchedul();
        omcSchedulQuery.setNursingGroupId(shiftLogDto.getNursingGroupId());
        omcSchedulQuery.setMonth(shiftLogDto.getMonth());
        List<OmcShifts> omcShiftsList = omcRoomAssignmentMapper.queryShiftsListMonth(omcSchedulQuery);
        omcShiftsList.get(0).setUserIdString(getLoginAuthDto().getUserId().toString());
        omcShiftsList.get(0).setUserNameString(getLoginAuthDto().getUserName());
        //return WrapMapper.ok(omcShiftsList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),omcShiftsList);
    }

    /**
     * 查询异常警报信息
     * @param: [shiftLogDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation
    @PostMapping(value = "/selectMainExc")
    public Wrapper selectMainExc(@RequestBody ShiftLogDto shiftLogDto,
                                 BindingResult br) {

        List<DeviceAlertReqVo> deviceAlertReqVoList = omcShiftLogService.selectMainExc(convert(shiftLogDto),getLoginAuthDto());

        //处理成字符串格式
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<=deviceAlertReqVoList.size()-1;i++){
            sb.append(deviceAlertReqVoList.get(i).getLocation()+deviceAlertReqVoList.get(i).getAlertInfo());
            if(!(i == deviceAlertReqVoList.size()-1)){
                sb.append(";");
            }
        }

        //return WrapMapper.ok(sb.toString());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),sb.toString());
    }

    /**
     * 按日期查询交班日志
     *
     * @param: [shiftLogDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */

    @ValidateAnnotation
   // @LogAnnotation
    @PostMapping(value = "/selectByDate")
    public Wrapper selectByDate(@RequestBody ShiftLogDto shiftLogDto,
                                 BindingResult br) {
        OmcShiftLog omcShiftLog = convert(shiftLogDto);

        List<OmcShiftLog> omcShiftLogList = omcShiftLogService.selectByDate(omcShiftLog,getLoginAuthDto());

        //return WrapMapper.ok(omcShiftLogList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),omcShiftLogList);
    }

    /**
     * 交班日志-添加
     * @param: [shiftLogDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation
    @PostMapping(value = "/add")
    public Wrapper shiftLogListAdd(@Validated(ShiftLogDtoAddValid.class)
                                @RequestBody List<ShiftLogDto> shiftLogDtoList,
                                BindingResult br) {

        //校验集合是否为空
        if(shiftLogDtoList == null || shiftLogDtoList.size() < 1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("shiftLogDtoList.size.null", null));
        }

        //循环拷贝属性值
        List<OmcShiftLog> omcShiftLogList = new LinkedList<>();
        for(ShiftLogDto shiftLogDto : shiftLogDtoList){
            OmcShiftLog omcShiftLog = new OmcShiftLog();
            BeanUtils.copyProperties(shiftLogDto,omcShiftLog);
            omcShiftLogList.add(omcShiftLog);
//            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
//                    .getMessage("operation.success", null),omcShiftLog);
        }

        //保存
        int ret = omcShiftLogService.batchAddShiftLog(omcShiftLogList,getLoginAuthDto());
        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 交班日志-修改
     * @param: [shiftLogDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation
    @PostMapping(value = "/edit")
    public Wrapper shiftLogListEdit(@Validated(ShiftLogDtoEditValid.class)
                                   @RequestBody List<ShiftLogDto> shiftLogDtoList,
                                   BindingResult br) {

        //校验集合是否为空
        if(shiftLogDtoList == null || shiftLogDtoList.size() < 1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("shiftLogDtoList.size.null", null));
        }

        //循环拷贝属性值
        List<OmcShiftLog> omcShiftLogList = new LinkedList<>();
        for(ShiftLogDto shiftLogDto : shiftLogDtoList){
            OmcShiftLog omcShiftLog = new OmcShiftLog();
            BeanUtils.copyProperties(shiftLogDto,omcShiftLog);
            omcShiftLogList.add(omcShiftLog);
        }

        //保存
        int ret = omcShiftLogService.batchUpdateShiftLog(omcShiftLogList,getLoginAuthDto());
        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /*
    * dto转化为pojo
    * */
    public OmcShiftLog convert(ShiftLogDto shiftLogDto){
        OmcShiftLog omcShiftLog = new OmcShiftLog();
        BeanUtils.copyProperties(shiftLogDto,omcShiftLog);
        return omcShiftLog;
    }

/*    *//*
    * 交班计划-保存
    * *//*
    public int save(ShiftLogDto shiftLogDto){
        OmcShiftLog omcShiftLog = new OmcShiftLog();
        BeanUtils.copyProperties(shiftLogDto,omcShiftLog);
        return omcShiftLogService.saveShiftLog(omcShiftLog,getLoginAuthDto());
    }*/
}
